<?php
// +----------------------------------------------------------------------
// | Description: 审批
// +----------------------------------------------------------------------
// | Author: Michael_xu | gengxiaoxu@5kcrm.com 
// +----------------------------------------------------------------------

namespace app\oa\controller;

use app\admin\controller\ApiCommon;
use app\crm\model\Accounting;
use app\crm\model\Receivables;
use think\Hook;
use think\Request;
use think\Db;

class Examine extends ApiCommon
{
    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @other 其他根据系统设置
    **/    
    public function _initialize()
    {
        $action = [
            'permission'=>[''],
            'allow'=>['index','save','read','update','delete','categorylist','check','revokecheck','category','categorysave','categoryupdate','categorydelete','categoryenables']            
        ];
        Hook::listen('check_auth',$action);
        $request = Request::instance();
        $a = strtolower($request->action());        
        if (!in_array($a, $action['permission'])) {
            parent::_initialize();
        }
        $userInfo = $this->userInfo;
        //权限判断
        $unAction = ['index','save','read','update','delete','categorylist','check','revokecheck'];
        $adminTypes = adminGroupTypes($userInfo['id']);
        if (!in_array(5,$adminTypes) && !in_array(1,$adminTypes) && !in_array(2,$adminTypes) && !in_array($a, $unAction)) {
            header('Content-Type:application/json; charset=utf-8');
            exit(json_encode(['code'=>102,'error'=>'无权操作']));
        }        
    }

    /**
     * 审批列表
     * @author Michael_xu
     * @return 
     */
    public function index()
    {
        $examineModel = model('Examine');
        $param = $this->param;
        $userInfo = $this->userInfo;
        $param['user_id'] = $userInfo['id'];

        $data = $examineModel->getDataList($param);       
        return resultArray(['data' => $data]);
    }
    //退款流程获取
    private function getRefoundExamineCategory($refound_type="",$structure_id){
        $expenceExamine=db("oa_examine_category")
            ->where(["title"=>["like","%退款%"],"status"=>1,"is_deleted"=>0])
            ->field("category_id,structure_ids,performance")->order("money desc")->select();//退款
        if($expenceExamine){
            foreach ($expenceExamine as $k=>$v){
                $structure=$v["structure_ids"]?in_array($structure_id,explode(',',$v["structure_ids"])):"1";
                if((trim($v["performance"])==trim($refound_type) || $v["performance"]=="全部") && $structure){
                    return $v["category_id"];
                }
            }
        }
        return "";
    }
    /**
     * 添加审批
     * @author Michael_xu
     * @param 
     * @return 
     */
    public function save()
    {
        $examineModel = model('Examine');
        $param = $this->param;
        $userInfo = $this->userInfo;
        $param['create_user_id'] = $userInfo['id'];
        $category_id = $param['category_id'];
        if($category_id==12){//退款判断走哪一个审批流程
            $category_id=$this->getRefoundExamineCategory($param["crm_upqdbn"],$userInfo["structure_id"]);
            if(!$category_id) return resultArray(['error' => '没有可用的退款审批流！']);
        }
        $param['category_id'] = $category_id;
        $contract=\app\crm\model\Contract::alias("contract")->where(["contract_id"=>$param['contract_ids'][0]])->find();//订单
        if($contract["check_status"]!=2){
            return resultArray(['error' => '订单未审核通过，不能操作']);
        }
        //审核判断（是否有符合条件的审批流）
        $examineFlowModel = new \app\admin\model\ExamineFlow();
        $examineStepModel = new \app\admin\model\ExamineStep();
        if (!$examineFlowModel->checkExamine($param['create_user_id'], 'oa_examine', $category_id)) {
            return resultArray(['error' => '暂无审批人，无法创建']); 
        }
        //获取审批相关信息
        $examineFlowData = $examineFlowModel->getFlowByTypes($param['create_user_id'], 'oa_examine', $category_id);

        if (!$examineFlowData) {
            return resultArray(['error' => '无可用审批流，请联系管理员']);
        }
        $param['flow_id'] = $examineFlowData['flow_id'];
        //获取审批人信息
        if ($examineFlowData['config'] == 1) {
            //固定审批流
            $nextStepData = $examineStepModel->nextStepUser($userInfo['id'], $examineFlowData['flow_id'], 'oa_examine', 0, 0, 0);
            $check_user_id = $nextStepData['next_user_ids'] ? : '';
            $param['order_id'] = 1;
        } else {
            $check_user_id = $param['check_user_id'] ? ','.$param['check_user_id'].',' : '';
        }
        if (!$check_user_id) {
            return resultArray(['error' => '无可用审批人，请联系管理员']);
        }
        $param['check_user_id'] = is_array($check_user_id) ? ','.implode(',',$check_user_id).',' : $check_user_id; 
        //流程审批人
        // $flow_user_id = $examineFlowModel->getUserByFlow($examineFlowData['flow_id'], $userInfo['id']); 
        // $param['flow_user_id'] = $flow_user_id ? arrayToString($flow_user_id) : ''; 
        $res = $examineModel->createData($param);
        if ($res) {
            $categoryModel = new \app\oa\model\ExamineCategory();
            $categoryInfo = $categoryModel->getDataById($category_id);
            actionLog($res['examine_id'], '', '', '创建了审批');
            return resultArray(['data' => '添加成功']);
        } else {
            return resultArray(['error' => $examineModel->getError()]);
        }
    }

    /**
     * 审批详情
     * @author Michael_xu
     * @param  
     * @return
     */
    public function read()
    {
        $examineModel = model('Examine');
        $userModel = new \app\admin\model\User();
        $examineFlowModel = new \app\admin\model\ExamineFlow();
        $param = $this->param;
        $userInfo = $this->userInfo;
        $data = $examineModel->getDataById($param['id']);
        //权限判断(创建人、审批人、管理员)
        $adminIds = $userModel->getAdminId(); //管理员
        $checkUserIds = $examineFlowModel->getUserByFlow($data['flow_id'], $data['create_user_id'], $data['check_user_id']);
        /*if (($userInfo['id'] != $data['create_user_id']) && !in_array($userInfo['id'],$adminIds) && !in_array($userInfo['id'],$checkUserIds)) {
            return resultArray(['error' => '没有权限']);
        }*/
        if (!$data) {
            return resultArray(['error' => $examineModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }

    /**
     * 编辑审批
     * @author Michael_xu
     * @param 
     * @return
     */
    public function update()
    {
        $examineModel = model('Examine');
        $param = $this->param;
        $userInfo = $this->userInfo;
        $dataInfo = db('oa_examine')->where(['examine_id' => $param['id']])->find();
        if (!$dataInfo) {
            return resultArray(['error' => '数据不存在或已删除']); 
        }
        //权限判断
        if ($userInfo['id'] != $dataInfo['create_user_id']) {
            return resultArray(['error' => '没有权限']);
        }
        if (in_array($dataInfo['check_status'],['2'])) {
            return resultArray(['error' => '审核通过订单，不可编辑']);
        }
        //审核判断（是否有符合条件的审批流）
        $examineFlowModel = new \app\admin\model\ExamineFlow();
        $examineStepModel = new \app\admin\model\ExamineStep();
        if (!$examineFlowModel->checkExamine($dataInfo['create_user_id'], 'oa_examine', $dataInfo['category_id'])) {
            return resultArray(['error' => '暂无审批人，无法创建']); 
        }
        //获取审批相关信息
        $examineFlowData = $examineFlowModel->getFlowByTypes($dataInfo['create_user_id'], 'oa_examine', $dataInfo['category_id']);
        if (!$examineFlowData) {
            return resultArray(['error' => '无可用审批流，请联系管理员']);
        }
        $param['flow_id'] = $examineFlowData['flow_id'];
        //获取审批人信息
        if ($examineFlowData['config'] == 1) {
            //固定审批流
            $nextStepData = $examineStepModel->nextStepUser($dataInfo['create_user_id'], $examineFlowData['flow_id'], 'oa_examine', 0, 0, 0);
            $next_user_ids = arrayToString($nextStepData['next_user_ids']) ? : '';
            $check_user_id = $next_user_ids ? : '';
            $param['order_id'] = 1;
        } else {
            $check_user_id = $param['check_user_id'] ? ','.$param['check_user_id'].',' : '';
        }
        if (!$check_user_id) {
            return resultArray(['error' => '无可用审批人，请联系管理员']);
        }
        $param['check_user_id'] = is_array($check_user_id) ? ','.implode(',',$check_user_id).',' : $check_user_id; 
        $param['check_status'] = 0;
        //流程审批人
        // $flow_user_id = $examineFlowModel->getUserByFlow($examineFlowData['flow_id'], $dataInfo['create_user_id']); 
        // $param['flow_user_id'] = $flow_user_id ? arrayToString($flow_user_id) : ''; 
        $param['flow_user_id'] = '';                   

        $res = $examineModel->updateDataById($param, $param['id']);
        if ($res) {
            //将审批记录至为无效
            $examineRecordModel = new \app\admin\model\ExamineRecord();
            $examineRecordModel->setEnd(['types' => 'oa_examine','types_id' => $param['id']]);            
            $categoryModel = new \app\oa\model\ExamineCategory();
            $categoryInfo = $categoryModel->getDataById($dataInfo['category_id']);            
            actionLog($param['id'], '', '', '新建了审批');
            return resultArray(['data' => '编辑成功']);
        } else {
            return resultArray(['error' => $examineModel->getError()]);
        } 
    }

    /**
     * 删除审批（逻辑删）
     * @author Michael_xu
     * @param 
     * @return
     */
    public function delete()
    {
        $examineModel = model('Examine');
        $param = $this->param;
        $userInfo = $this->userInfo;
        $dataInfo = db('oa_examine')->where(['examine_id' => $param['id']])->find();
        if (!$dataInfo) {
            return resultArray(['error' => '数据不存在或已删除']); 
        }
        $adminTypes = adminGroupTypes($userInfo['id']);
        if (in_array($dataInfo['check_status'], [2,3])) {
            return resultArray(['error' => '已审批，不可删除']);  
        }
        if (!in_array($dataInfo['check_status'],['4']) && !in_array(1,$adminTypes)) {
            return resultArray(['error' => '不可删除，请先撤销审核']);  
        }         
        //权限判断
        if ($userInfo['id'] != $dataInfo['create_user_id']) {
            return resultArray(['error' => '无权操作']);
        }
        $data = $examineModel->delDataById($param['id']);
        if (!$data) {
            return resultArray(['error' => $examineModel->getError()]);
        }       
        actionLog($param['id'], '', '', '删除了审批');
        return resultArray(['data' => '删除成功']);
    }

    /**
     * 审批类型(列表)
     * @author Michael_xu
     * @param 
     * @return
     */ 
    public function category()
    {
        $categoryModel = model('ExamineCategory');
        $param = $this->param;
        $data = $categoryModel->getDataList($param);       
        return resultArray(['data' => $data]);
    }

    /**
     * 审批类型(创建)
     * @author Michael_xu
     * @param 
     * @return
     */ 
    public function categorySave()
    {
        $categoryModel = model('ExamineCategory');
        $param = $this->param;
        $userInfo = $this->userInfo;
        $param['create_user_id'] = $userInfo['id'];
        
        $res = $categoryModel->createData($param);
        if ($res) {
            return resultArray(['data' => $res]);
        } else {
            return resultArray(['error' => $categoryModel->getError()]);
        }
    }

    /**
     * 审批类型(编辑)
     * @author Michael_xu
     * @param 
     * @return
     */
    public function categoryUpdate()
    {
        $categoryModel = model('ExamineCategory');
        $examineFlowModel = new \app\admin\model\ExamineFlow();
        $examineStepModel = new \app\admin\model\ExamineStep();     
        $param = $this->param;
        $userInfo = $this->userInfo;

        $category_id = $param['id'];
        $dataInfo = $categoryModel->getDataById($category_id);
        if (!$dataInfo) {
            return resultArray(['error' => '数据不存在或已删除']);
        } 
        //将当前审批流标记为已删除，重新创建审批流(目的：保留审批流程记录)
        // $newData = db('admin_examine_flow')->where(['flow_id' => $dataInfo['flow_id']])->find();
        
        $param['name'] = $param['title'].'流程';
        $param['types'] = 'oa_examine';        
        $param['types_id'] = $category_id;        
        $param['user_ids'] = arrayToString($param['user_ids']);
        $param['structure_ids'] = arrayToString($param['structure_ids']);        
        $param['update_user_id'] = $userInfo['id'];
        $param['create_time'] = time();
        $param['update_time'] = time();
        $param['status'] = 1;
        $resUpdate = $examineFlowModel->createData($param);

        if ($resUpdate) {
            if ($param['config'] == 1) {
                $resStep = $examineStepModel->createStepData($param['step'], $resUpdate['flow_id']);
                if (!$resStep) {
                    return resultArray(['error' => $examineStepModel->getError()]);
                }  
            }
            if ($dataInfo['flow_id']) {
                $upData = [];
                $upData['is_deleted'] = 1;      
                $upData['delete_time'] = time();      
                $upData['delete_user_id'] = $userInfo['id'];      
                $upData['status'] = 0;
//              var_dump($dataInfo['flow_id']);exit;
                $resFlow = db('admin_examine_flow')->where(['flow_id' => $dataInfo['flow_id']])->update($upData);
                if (!$resFlow) {
                    return resultArray(['error' => '编辑失败']);
                }
            }

            $param['flow_id'] = $resUpdate['flow_id'];
            $res = $categoryModel->updateDataById($param, $param['id']);

            if (!$res) {
                return resultArray(['error' => $categoryModel->getError()]);
            }
            return resultArray(['data' => '编辑成功']);
        } else {
            return resultArray(['error' => $examineFlowModel->getError()]);
        } 
    }

    /**
     * 审批类型（逻辑删）
     * @author Michael_xu
     * @param 
     * @return
     */
    public function categoryDelete()
    {
        $categoryModel = model('ExamineCategory');
        $param = $this->param;
        $userInfo = $this->userInfo;
        $data = $categoryModel->signDelById($param['id'], $userInfo['id']);
        if (!$data) {
            return resultArray(['error' => $categoryModel->getError()]);
        }
        return resultArray(['data' => '删除成功']);
    }

    /**
     * 审批类型状态（启用、停用）
     * @author Michael_xu
     * @param ids array
     * @param status 1启用，0禁用
     * @return
     */    
    public function categoryEnables()
    {
        $categoryModel = model('ExamineCategory');
        $param = $this->param;
        $userInfo = $this->userInfo;
        $id = [$param['id']];
        $data = $categoryModel->enableDatas($id, $param['status']);  
        if (!$data) {
            return resultArray(['error' => $categoryModel->getError()]);
        } 
        return resultArray(['data' => '操作成功']);         
    }      

    /**
     * 审批类型列表(创建时)
     * @author Michael_xu
     * @return 
     */
    public function categoryList()
    {
        $param = $this->param;
        $userInfo = $this->userInfo;
        $where = [];
        $where['is_deleted'] = ['neq',1];
        $where['status'] = ['eq',1];
        // $where['category_id'] = array('not in',array(12,13,14,15));//新增
        $list = db('oa_examine_category')
            ->where($where)
        //     ->where(function ($query) {
        //         $query->where(['user_ids' => '','structure_ids' => '']);
        //     })->whereOr(function($query) use ($userInfo){
        //         $query->where('structure_ids','like',','.$userInfo['structure_id'].',')
        //               ->whereOr('user_ids','like',','.$userInfo['id'].',');
        // })
            ->select();
        return resultArray(['data' => $list]);        
    } 

    /**
     * 审批审核
     * @author Michael_xu
     * @param 
     * @return
     */  
    public function check()
    {
        $param = $this->param;
        $userInfo = $this->userInfo;
        $user_id = $userInfo['id'];
        $examineModel = model('Examine');
        $examineStepModel = new \app\admin\model\ExamineStep();
        $examineRecordModel = new \app\admin\model\ExamineRecord();
        $examineFlowModel = new \app\admin\model\ExamineFlow();

        $examineData = [];
        $examineData['update_time'] = time();
        $examineData['check_status'] = 1; //0待审核，1审核通中，2审核通过，3审核未通过
        //权限判断
        if (!$examineStepModel->checkExamine($user_id, 'oa_examine', $param['id'])) {
           return resultArray(['error' => $examineStepModel->getError()]); 
        };
        //审批主体详情
        $dataInfo = $examineModel->getDataById($param['id']);
        $flowInfo = $examineFlowModel->getDataById($dataInfo['flow_id']);
        $is_end = 0; // 1审批结束

        $status = $param['status'] ? 1 : 0; //1通过，0驳回
        $checkData = [];
        $checkData['check_user_id'] = $user_id;
        $checkData['types'] = 'oa_examine';
        $checkData['types_id'] = $param['id'];
        $checkData['check_time'] = time();
        $checkData['content'] = $param['content'];
        $checkData['flow_id'] = $dataInfo['flow_id'];
        $checkData['order_id'] = $dataInfo['order_id'] ? : 1;
        $checkData['status'] = $status;


        if ($status == 1) {
            if ($flowInfo['config'] == 1) {
                //固定流程
                //获取下一审批信息
                $nextStepData = $examineStepModel->nextStepUser($dataInfo['create_user_id'], $dataInfo['flow_id'], 'oa_examine', $param['id'], $dataInfo['order_id'], $user_id);
                $next_user_ids = $nextStepData['next_user_ids'] ? : [];
                $examineData['order_id'] = $nextStepData['order_id'] ? : '';
                if (!$next_user_ids) {
                    $is_end = 1;
                    //审批结束
                    $checkData['check_status'] = !empty($status) ? 2 : 3;
                    $examineData['check_user_id'] = '';
                } else {
                    //修改主体相关审批信息
                    $examineData['check_user_id'] = arrayToString($next_user_ids);
                }                 
            } else {
                //自选流程
                $is_end = $param['is_end'] ? 1 : '';
                $check_user_id = $param['check_user_id'] ? : '';
                if ($is_end !== 1 && empty($check_user_id)) {
                    return resultArray(['error' => '请选择下一审批人']); 
                }
                $examineData['check_user_id'] = arrayToString($param['check_user_id']);
            } 
            if ($is_end == 1) {
                $checkData['check_status'] = !empty($status) ? 2 : 3;
                $examineData['check_user_id'] = '';
                $examineData['check_status'] = 2;
            }
            //新增 lina 成本变更 end
        } else {
            //审批驳回
            $is_end = 1;
            $examineData['check_status'] = 3;
            //将审批记录至为无效
            // $examineRecordModel->setEnd(['types' => 'oa_examine','types_id' => $param['id']]);
        }
        //-------------------------更新财务表数据start-------------------------------
        $expenceExamine=db("oa_examine_category")->where(["title"=>["like","%报销%"],"status"=>1,"is_deleted"=>0])->column("category_id");//报销
        //报销专用
        if(in_array($dataInfo["category_id"],$expenceExamine)){
            $financeUpdateData["check_status"]=$examineData['check_status'];//审核状态
            if($is_end==1 && $param['status']==1){//判断是否是最后一层审批并且是同意
                $financeUpdateData["check_status"]=2;//审核状态修改为审核通过
            }
            $financeUpdateData["check_time"]=date("Y-m-d H:i:s");//审核时间
            $financeUpdateData["remarks"]=$param['content'];//备注
            db("crm_finance")->where(["relation_id"=>$dataInfo['examine_id'],"order_type"=>"报销"])->update($financeUpdateData);
            db("crm_expence")->where(["examine_id"=>$dataInfo['examine_id']])->update(["check_time"=>$financeUpdateData["check_time"],"status"=>$financeUpdateData["check_status"]]);
        }
        //开票专用
        $invoiceExamine=db("oa_examine_category")->where(["title"=>["like","%开票%"],"status"=>1,"is_deleted"=>0])->column("category_id");//开票
        if(in_array($dataInfo["category_id"],$invoiceExamine)){
            $invoiceUpdateData["status"]=$examineData['check_status'];//审核状态
            if($is_end==1 && $param['status']==1){//判断是否是最后一层审批并且是同意
                $invoiceUpdateData["status"]=2;//审核状态修改为审核通过
            }
            db("crm_invoice")->where(["examine_id"=>$dataInfo['examine_id']])->update($invoiceUpdateData);
            $invoiceDetail = db("crm_invoice")->where(["examine_id"=>$dataInfo['examine_id']])->find();
            if($invoiceDetail) {
                db("crm_invoice_detail")->where(["invoice_id"=>$invoiceDetail['id']])->update($invoiceUpdateData);
            }
        }

        $costExamine=db("oa_examine_category")->where(["title"=>"核定成本调整","status"=>1,"is_deleted"=>0])->column("category_id");//核定成本
        // $refundExamine=db("oa_examine_category")->where(["title"=>"退款申请","status"=>1,"is_deleted"=>0])->column("category_id");//退款申请
        $refundExamine=db("oa_examine_category")->whereLike("title", "%退款申请%")->where(["status"=>1,"is_deleted"=>0])->column("category_id");//退款申请
        $changeFinanceCode=array_merge($costExamine,$refundExamine);
        if(in_array($dataInfo["category_id"],$changeFinanceCode)){//12退款17核定成本变更
            $financeData=db("crm_finance")->where(["relation_id"=>$dataInfo['examine_id'],"check_status"=>["in",["0","1"]],"order_type"=>["in",["成本变更","报销","退款"]]])->find();
            if($financeData){
                if(in_array($dataInfo["category_id"],$changeFinanceCode) && $is_end==1 && $param['status']==1){//只有退款和核定成本变更有财务编号
                    $year=date("Y");
                    $month=date("m");
                    $financeNumber=Db::name("crm_finance")
                        ->where(["department"=>$financeData["department"],"check_status"=>"2","order_type"=>["in",["成本变更","退款","回款"]]])
                        ->where("YEAR(check_time)={$year} and MONTH(check_time)={$month} and calculate_way != '暂不核算'")
                        ->count();
                    $financeNumber=$financeNumber>0?str_pad($financeNumber+1,4,"0",STR_PAD_LEFT):"0001";//滚动号数
                    $financeUpdateData["order_sn"]=substr($financeData["department"],0,1)."-".date("Y")."-".date("m")."-".$financeNumber;//财务编号
                }

                $financeUpdateData["check_status"]=$examineData['check_status'];//审核状态
                if($is_end==1 && $param['status']==1){//判断是否是最后一层审批并且是同意
                    $financeUpdateData["check_status"]=2;//审核状态修改为审核通过
                }
                $financeUpdateData["check_time"]=date("Y-m-d H:i:s");//审核时间
                $financeUpdateData["remarks"]=$param['content'];//备注
                db("crm_finance")->where(["finance_id"=>$financeData['finance_id']])->update($financeUpdateData);
                // if(in_array($dataInfo["category_id"],$costExamine) && $is_end==1 && $param['status']==1){//成本变更时同步金额到对应的财务表和合同表
                //     // db("crm_receivables")->where(["number"=>$financeData["receivables_code"]])->setInc("crm_oeseng",$financeData["crm_oeseng"]);//同步回款表
                //     // db("crm_contract")->where(["contract_id"=>$financeData["contract_id"]])->setInc("crm_oeseng",$financeData["crm_oeseng"]);//同步合同表
                //     $receivables = db("crm_receivables")->where(["number"=>$financeData["receivables_code"]])->find();
                //     db("crm_receivables")->where(["number"=>$financeData["receivables_code"]])->setInc("crm_oeseng",$financeData["crm_oeseng"]);//同步回款表
                //     // db("crm_finance")->where(["relation_id"=>$receivables['receivables_id'],"order_type"=>"回款"])->setInc("crm_oeseng",$financeData["crm_oeseng"]);
                //     db("crm_contract")->where(["contract_id"=>$financeData["contract_id"]])->setInc("crm_oeseng",$financeData["crm_oeseng"]);//同步合同表
                // }
                // if(in_array($dataInfo["category_id"],$refundExamine) && $is_end==1 && $param['status']==1){//退款成功后订单总的核定成本减少
                //     db("crm_contract")->where(["contract_id"=>$financeData["contract_id"]])->setDec("crm_oeseng",$financeData["crm_oeseng"]);//同步合同表
                // }
            }
        }
        //-------------------------更新财务表数据end-------------------------------
        //已审批人ID
        $examineData['flow_user_id'] = stringToArray($dataInfo['flow_user_id']) ? arrayToString(array_merge(stringToArray($dataInfo['flow_user_id']),[$user_id])) : arrayToString([$user_id]);
        $resExamine = db('oa_examine')->where(['examine_id' => $param['id']])->update($examineData);
        if ($resExamine) {
            //审批记录
            $resRecord = $examineRecordModel->createData($checkData);
            //审核通过
            if ($is_end == 1 && !empty($status)) {
                //发送站内信
                $sendContent = '您的'.$dataInfo['category_name'].','.$userInfo['realname'].'已审核通过,审批结束';
                $resMessage = sendMessage($dataInfo['owner_user_id'], $sendContent, $param['id'], 1);

            } else {
                if ($status) {
                    //发送站内信
                    $sendContent = '您的'.$dataInfo['category_name'].','.$userInfo['realname'].'已审核通过';
                    $resMessage = sendMessage($dataInfo['owner_user_id'], $sendContent, $param['id'], 1);
                } else {
                    $sendContent = '您的'.$dataInfo['category_name'].','.$userInfo['realname'].'已审核拒绝,审核意见：'.$param['content'];
                    $resMessage = sendMessage($dataInfo['owner_user_id'], $sendContent, $param['id'], 1);
                }
            }
            return resultArray(['data' => '审批成功']);
        } else {
            return resultArray(['error' => '审批失败，请重试！']);
        }
    }

    /**
     * 审批撤销审核
     * @author Michael_xu
     * @param 
     * @return
     */  
    public function revokeCheck()
    {
        $param = $this->param;
        $userInfo = $this->userInfo;
        $user_id = $userInfo['id'];
        $examine_id = $param['id'];
        $examineModel = model('Examine');
        $examineRecordModel = new \app\admin\model\ExamineRecord();
        $userModel = new \app\admin\model\User();
        $examineData = [];
        $examineData['update_time'] = time();
        $examineData['check_status'] = 0; //0待审核，1审核通中，2审核通过，3审核未通过
        //审批主体详情
        $dataInfo = db('oa_examine')->where(['examine_id' => $examine_id])->find();

        //权限判断(创建人或管理员)
        if ($dataInfo['check_status'] == 2) {
            return resultArray(['error' => '已审批结束,不能撤销']);   
        } 
        if ($dataInfo['check_status'] == 4) {
            return resultArray(['error' => '无需撤销']);   
        }  
        $admin_user_ids = $userModel->getAdminId(); 
        if ($dataInfo['create_user_id'] !== $user_id && !in_array($user_id, $admin_user_ids)) {
            return resultArray(['error' => '没有权限']);
        }

        $expenceExamine=db("oa_examine_category")->where(["title"=>["like","%报销%"],"status"=>1,"is_deleted"=>0])->column("category_id");//报销
        $costExamine=db("oa_examine_category")->where(["title"=>"核定成本调整","status"=>1,"is_deleted"=>0])->column("category_id");//核定成本
        $refundExamine=db("oa_examine_category")->where(["title"=>["like","%退款%"],"status"=>1,"is_deleted"=>0])->column("category_id");//退款申请
        $invoiceExamine=db("oa_examine_category")->where(["title"=>["like","%开票%"],"status"=>1,"is_deleted"=>0])->column("category_id");//报销
        //撤回时同步财务表的审核状态 //报销判断、核定成本、退款申请、批量开票
        if(in_array($dataInfo["category_id"],array_merge($expenceExamine,$costExamine,$refundExamine,$invoiceExamine))){
            db("crm_finance")->where(["relation_id"=>$examine_id,"order_type"=>["in",["报销","成本变更","退款"]],"check_status"=>["in",["0","1"]]])->update(["check_status"=>4]);
            if(in_array($dataInfo["category_id"],$expenceExamine)){
                db("crm_expence")->where(["examine_id"=>$dataInfo['examine_id']])->update(["status"=>4]);
            }
            if(in_array($dataInfo["category_id"],$invoiceExamine)){
                db("crm_invoice")->where(["examine_id"=>$dataInfo['examine_id']])->update(["status"=>4]);
                $invoiceDetail = db("crm_invoice")->where(["examine_id"=>$dataInfo['examine_id']])->find();
                if($invoiceDetail) {
                    db("crm_invoice_detail")->where(["invoice_id"=>$invoiceDetail['id']])->update(["status"=>4]);
                }
            }
        }


        $is_end = 0; // 1审批结束
        $status = 2; //1通过，0驳回, 2撤销
        $checkData = [];
        $checkData['check_user_id'] = $user_id;
        $checkData['types'] = 'oa_examine';
        $checkData['types_id'] = $param['id'];
        $checkData['check_time'] = time();
        $checkData['content'] = $param['content'];
        $checkData['flow_id'] = $dataInfo['flow_id'];
        $checkData['order_id'] = $dataInfo['order_id'];
        $checkData['status'] = $status;
        
        $examineData['check_status'] = 4;
        $examineData['check_user_id'] = '';
        $examineData['flow_user_id'] = '';
        $resExamine = db('oa_examine')->where(['examine_id' => $examine_id])->update($examineData);
        if ($resExamine) {
            //将审批记录至为无效
            // $examineRecordModel->setEnd(['types' => 'oa_examine','types_id' => $examine_id]);
            //审批记录
            $resRecord = $examineRecordModel->createData($checkData);
            return resultArray(['data' => '撤销成功']);            
        } else {
            return resultArray(['error' => '撤销失败，请重试！']); 
        }
    }     
}
